package com.sentiment.database.bean;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.bson.Document;import org.bson.types.ObjectId;
import org.bson.types.ObjectId;

import com.sentiment.database.dbUtil.DbUtil;

public class Role_Permissions {

	public static final String dbName = "UserDB";
	public static final String collection = "Role_Permissions";
	
	private ObjectId _id;
	private String role_id;
	private List<String> permission_id;

	public Role_Permissions(String role_id,
			List<String> permission_id) {
		super();
		this.role_id = role_id;
		this.permission_id = permission_id;
	}

	public Document toDocument(){
		return new Document("role_id",role_id).append("permission_id", permission_id);
	}
	
	public Role_Permissions(Document doc){
		super();
		_id = doc.getObjectId("_id");
		role_id = doc.getString("role_id");
		permission_id = (List<String>)doc.get("permission_id");
	}

	public Role_Permissions() {
		// TODO Auto-generated constructor stub
	}

	public String get_id() {
		return _id.toString();
	}

	public void set_id(String _id) {
		this._id = new ObjectId(_id);
	}

	public String getRole_id() {
		return role_id;
	}

	public void setRole_id(String role_id) {
		this.role_id = role_id;
	}

	public List<String> getPermission_id() {
		return permission_id;
	}

	public void setPermission_id(List<String> permission_id) {
		this.permission_id = permission_id;
	}

	@Override
	public String toString() {
		return "Role_Permissions [_id=" + _id + ", role_id=" + role_id
				+ ", permission_id=" + permission_id + "]";
	}
	
	/**
	 * 通过 Role_id 获取 Role_Permissions
	 * @param role_id
	 * @return
	 */
	public Role_Permissions getRole_PermissionsByRole_id(String role_id) {
		DbUtil db = new DbUtil();
		db.openConnection(dbName, collection);
		
		Role_Permissions role_pers = new Role_Permissions(role_id,null);
		String[] keys = {"role_id"};
		Document t = role_pers.toDocument();
		t = db.query(t, keys);
		
		db.closeConnection();
		
		if(t == null)
			return null;
		
		return new Role_Permissions(t);
	}
	
	/**
	 * 根据 Role_id 获取 Permission
	 * @param role_id
	 * @return
	 */
	public List<Permission> getPermissionListByRole_id(String role_id){
		
		DbUtil db = new DbUtil();
		db.openConnection(dbName, collection);
		List<Permission> tlist = new ArrayList<>();
		
		Role_Permissions role_pers = new Role_Permissions();
		role_pers = role_pers.getRole_PermissionsByRole_id(role_id);
		
		if(role_pers == null)
			return null;
		
		List<String> per_id = role_pers.getPermission_id();
		
		
		for(int i = 0; i < per_id.size(); i++){
			
			Permission per = new Permission();
			per = per.getPermissionByPermissionId(per_id.get(i));
			
			if(per != null)
				tlist.add(per);
		}
		db.closeConnection();
		return tlist;		
	}
	
	/**
	 * 删除角色对应的 Role_Permission 数据
	 * 使用前需确认 Role 存在于数据库中
	 * @param role
	 * @return
	 */
	public boolean deleteRole_PersByRole(Role role){
		
		DbUtil db = new DbUtil();
		db.openConnection(dbName, collection);
		
		Role_Permissions role_pers = new Role_Permissions();
		role_pers = role_pers.getRole_PermissionsByRole_id(role.get_id());
		String []keys = {"role_id"};
		boolean removed = db.remove(role_pers.toDocument(), keys);
		
		db.closeConnection();
		return removed;
	}
	
	/**
	 * 删除 Role_Permissions 表中所有 Permission
	 * @param per
	 * @return
	 */
	public boolean deletePermission(Permission per){
		
		DbUtil db = new DbUtil();
		db.openConnection(dbName, collection);
		
		boolean remove = db.remove("permission_id", per.get_id());
		
		db.closeConnection();
		
		return remove;
	}
	
	public static void main(String[] args) {
		
		Role_Permissions role_pers = new Role_Permissions();
		role_pers.setRole_id("56333ebb49e1b21f5444e293");
		role_pers.setPermission_id(Arrays.asList("56333ebb49e1b21f5444e298"));
		String []keys = {"role_id","permission_id"};
		DbUtil db = new DbUtil();
		db.openConnection(dbName, collection);
		List<Document> tlist = db.queryMany(role_pers.toDocument(), keys);
		db.closeConnection();
		System.out.println(tlist.size());
	}
	
	/**
	 * 用于测试时向数据库中写入数据
	 */
	public void test() {
		
		DbUtil db = new DbUtil();
		db.openConnection(dbName, collection);
		
		Role role = new Role();
		role = role.getRoleByRoleName("nopay");
		List<String> pers_id = new ArrayList<>();
		Permission per = new Permission();
		List<String> persName = new ArrayList<>();
		
//		persName.add("dailySentiment");//每日舆情列表
//		persName.add("dailySentimentDetail");//每日舆情详情
//		persName.add("hotSentiment");//热点舆情
//		persName.add("hotSentimentDetail");//热点舆情详情
		persName.add("hotInfoSupervision");//热点信息监督
		persName.add("plagiarismGraph");//舆情图表统计
		persName.add("plagiarismList");//抄袭检测搜索结果
		persName.add("plagiarismText");//盗版文本监测
		persName.add("plagiarismUrl");//盗版Url检测
		persName.add("sentimentList");//热点信息监督搜索结果
		persName.add("correlationInfo");//舆情监测-关联信息查询
		persName.add("leftmenu");//菜单
		persName.add("topbar");//页面顶部信息栏
		persName.add("user");//用户个人信息
		
		for(int i = 0; i < persName.size();i++){
			Permission permission = per.getPermissionByPermissionName(persName.get(i));
			pers_id.add(permission.get_id());
		}		
		Role_Permissions role_pres = new Role_Permissions();
		role_pres = new Role_Permissions(role.get_id(),pers_id);
		Document t = role_pres.toDocument();
		db.insert(t);
		
		
		role = role.getRoleByRoleName("pay");
		//persName.clear();
		pers_id.clear();
			
			
		for(int i = 0; i < persName.size();i++){
			Permission permission = per.getPermissionByPermissionName(persName.get(i));
			pers_id.add(permission.get_id());
		}		

		role_pres = new Role_Permissions(role.get_id(),pers_id);
		t = role_pres.toDocument();
		db.insert(t);
		
		role = role.getRoleByRoleName("admin");
		//persName.clear();
		pers_id.clear();
		persName.add("dailypublish");//每日舆情发布
		
		persName.add("hotpublish");//热点舆情发布
		persName.add("index");//后台首页
		persName.add("permissionlist");//权限列表
		persName.add("rolelist");//角色列表
		persName.add("userlist");//用户列表
		persName.add("systemedit");//系统信息编辑
		persName.add("systemrefer");//系统信息查看
		persName.add("userPaymentsRecord");//用户缴费历史
		persName.add("viewDailyPublishDetail");//每日舆情报告预览
		persName.add("viewUserDetail");//查看用户信息详情
		persName.add("historyrecorddaily");//每日舆情历史信息
		persName.add("historyrecordhot");//热点舆情历史信息
		persName.add("dailypublishEdit");//编辑每日报告
			
		for(int i = 0; i < persName.size();i++){
			Permission permission = per.getPermissionByPermissionName(persName.get(i));
			pers_id.add(permission.get_id());
		}		

		role_pres = new Role_Permissions(role.get_id(),pers_id);
		t = role_pres.toDocument();
		db.insert(t);
		
		
		
		db.closeConnection();
		
	}
}
